home *** CD-ROM | disk | FTP | other *** search
/ Pocket PC Game Programming / Pocket PC Game Programming.iso / source.exe / CH15 / GameLibrary / CSprite.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-03-04  |  5.1 KB  |  211 lines

  1. //////////////////////////////////////////////////////////////////////
  2. // Pocket PC Game Programming
  3. // Chapter 9: Sprites and Animation
  4. //
  5. // CSprite Source File
  6. //
  7. // This file includes the CSprite class implementation.
  8. //
  9. //////////////////////////////////////////////////////////////////////
  10.  
  11. #include "stdafx.h"
  12. #include "CBitmap.h"
  13. #include "CSprite.h"
  14.  
  15.  
  16. //////////////////////////////////////////////////////////////////////
  17. // CSprite Constructor
  18. //
  19. // This function is called when the class is instantiated.
  20. //
  21. CSprite::CSprite():CBitmap()
  22. {
  23.     Init();
  24. }
  25.  
  26. //////////////////////////////////////////////////////////////////////
  27. // CSprite Constructor (Overloaded)
  28. //
  29. // This function is called when the class is instantiated.
  30. //
  31. CSprite::CSprite(HDC hdc):CBitmap(hdc)
  32. {
  33.     Init();
  34. }
  35.  
  36. void CSprite::Init()
  37. {
  38.     X_Loc = 0;
  39.     Y_Loc = 0;
  40.     X_Dir = 1;
  41.     Y_Dir = 1;
  42.     X_Spd = 1;
  43.     Y_Spd = 1;
  44.  
  45.     bUnderSaved = FALSE;
  46.     bAlive = FALSE;
  47.  
  48.     DestRect = new RECT;
  49.     Rect1 = new RECT;
  50.     Rect2 = new RECT;
  51. }
  52.  
  53.  
  54. //////////////////////////////////////////////////////////////////////
  55. // CSprite Destructor
  56. //
  57. // This function is called when the class is terminated.
  58. //
  59. CSprite::~CSprite()
  60. {
  61.     DeleteDC(hdcUnder);
  62.  
  63.     delete DestRect;
  64.     delete Rect1;
  65.     delete Rect2;
  66. }
  67.  
  68. //////////////////////////////////////////////////////////////////////
  69. // CSprite::SaveUnder
  70. //
  71. // Saves the background image under the sprite.
  72. //
  73. BOOL CSprite::SaveUnder(HDC hdc)
  74. {
  75.     if (!bUnderSaved)
  76.     {
  77.         hUnder = CreateCompatibleBitmap(hdc, ImageWidth(), ImageHeight());
  78.         if (hUnder == NULL)
  79.             return FALSE;
  80.  
  81.         hdcUnder = CreateCompatibleDC(hdc);
  82.         if (hdcUnder == NULL)
  83.             return FALSE;
  84.  
  85.         SelectObject(hdcUnder, hUnder);
  86.         DeleteObject(hUnder);
  87.         bUnderSaved = TRUE;
  88.     }
  89.  
  90.     if (hdcUnder != 0 && hdc != 0)
  91.         BitBlt(hdcUnder, 0, 0, ImageWidth(), ImageHeight(), hdc, GetX(), GetY(), SRCCOPY);
  92.  
  93.     return TRUE;
  94. }
  95.  
  96. //////////////////////////////////////////////////////////////////////
  97. // CSprite::RestoreUnder
  98. //
  99. // Restores the background image that was under the sprite.
  100. //
  101. BOOL CSprite::RestoreUnder(HDC hdc)
  102. {
  103.     if (hdcUnder != 0 && hdc != 0)
  104.         BitBlt(hdc, GetX(), GetY(), ImageWidth(), ImageHeight(), hdcUnder, 0, 0, SRCCOPY);
  105.     else
  106.         return FALSE;
  107.  
  108.     return TRUE;
  109. }
  110.  
  111. //////////////////////////////////////////////////////////////////////
  112. // CSprite::TransBlit
  113. //
  114. // Draws the sprite transparently.
  115. //
  116. BOOL CSprite::TransBlit(COLORREF clrTrans)
  117. {
  118.     return CBitmap::TransBlit(GetX(), GetY(), clrTrans);
  119. }
  120.  
  121. //////////////////////////////////////////////////////////////////////
  122. // CSprite::BitBlit
  123. //
  124. // Draws the sprite to the destination DC already specified.
  125. //
  126. BOOL CSprite::BitBlit()
  127. {
  128.     return CBitmap::BitBlit(GetX(), GetY());
  129. }
  130.  
  131. //////////////////////////////////////////////////////////////////////
  132. // CSprite::BitBlit (Overloaded)
  133. //
  134. // Draws the sprite to the destination DC parameter.
  135. //
  136. BOOL CSprite::BitBlit(HDC hdc)
  137. {
  138.     return CBitmap::BitBlit(hdc, GetX(), GetY());
  139. }
  140.  
  141. //////////////////////////////////////////////////////////////////////
  142. // CSprite::StretchBlit
  143. //
  144. // Draws a scaled sprite to the destination.
  145. //
  146. BOOL CSprite::StretchBlit(int dx, int dy)
  147. {
  148.     return CBitmap::StretchBlit(GetX(), GetY(), dx, dy);
  149. }
  150.  
  151. //////////////////////////////////////////////////////////////////////
  152. // CSprite::Intersected
  153. //
  154. // Checks to see if this sprite has collided with another.
  155. // The POINT struct is returned with the center point of the
  156. // bounding rectangle that formed the union of the two sprites.
  157. //
  158. BOOL CSprite::Intersected(CSprite *Sprite, POINT &pt)
  159. {
  160.     RECT rt;
  161.  
  162.     if (Intersected(Sprite, rt))
  163.     {
  164.         pt.x = (rt.left + rt.right) / 2;
  165.         pt.y = (rt.top + rt.bottom) / 2;
  166.         return TRUE;
  167.     }
  168.  
  169.     return FALSE;
  170. }
  171.  
  172. //////////////////////////////////////////////////////////////////////
  173. // CSprite::Intersected
  174. //
  175. // Checks to see if this sprite has collided with another.
  176. // This version returns the actual union rectangle.
  177. //
  178. BOOL CSprite::Intersected(CSprite *Sprite, RECT &rt)
  179. {
  180.     int iShrinkX, iShrinkY;
  181.  
  182.     iShrinkX = ImageWidth() >> 2;
  183.     iShrinkY = ImageHeight() >> 2;
  184.  
  185.     Rect1->left = GetX() + iShrinkX;
  186.     Rect1->top = GetY() + iShrinkY;
  187.     Rect1->right = GetX() + ImageWidth() - iShrinkX;
  188.     Rect1->bottom = GetY() + ImageHeight() - iShrinkY;
  189.  
  190.     iShrinkX = Sprite->ImageWidth() >> 2;
  191.     iShrinkY = Sprite->ImageHeight() >> 2;
  192.  
  193.     Rect2->left = Sprite->GetX() + iShrinkX;
  194.     Rect2->top = Sprite->GetY() + iShrinkY;
  195.     Rect2->right = Sprite->GetX() + Sprite->ImageWidth() - iShrinkX;
  196.     Rect2->bottom = Sprite->GetY() + Sprite->ImageHeight() - iShrinkY;
  197.  
  198.     if (IntersectRect(DestRect, Rect1, Rect2))
  199.     {
  200.         rt.left = DestRect->left;
  201.         rt.right = DestRect->right;
  202.         rt.top = DestRect->top;
  203.         rt.bottom = DestRect->bottom;
  204.         return TRUE;
  205.     }
  206.  
  207.     return FALSE;
  208. }
  209.  
  210.  
  211.